package com.avcdata.aoscustommade.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.avcdata.national.network.BaseLog;
import com.google.gson.Gson;

import java.util.Set;

/**
 * Created by c.c on 2017/2/26.
 * 共享参数类
 */
public class SharedPreferencesUtils {
    private SharedPreferences userInfoSp;
    private SharedPreferences deviceSp;
    private SharedPreferences installSp;
    private SharedPreferences otherSp;

    private SharedPreferences.Editor userInfoEditor;
    private SharedPreferences.Editor deviceEditor;
    private SharedPreferences.Editor installEditor;
    private SharedPreferences.Editor otherEditor;

    public SharedPreferencesUtils(Context context) {
        userInfoSp = context.getSharedPreferences("user_info", Context.MODE_PRIVATE);
        userInfoEditor = userInfoSp.edit();

        deviceSp = context.getSharedPreferences("device_info",Context.MODE_PRIVATE);
        deviceEditor = deviceSp.edit();

        installSp = context.getSharedPreferences("install_info",Context.MODE_PRIVATE);
        installEditor = installSp.edit();

        otherSp = context.getSharedPreferences("other_info",Context.MODE_PRIVATE);
        otherEditor = otherSp.edit();
    }
    //清除用户信息
    public void clearUserInfo() {
        userInfoEditor.clear().commit();
    }

    //账号
    public void setUserAccount(String account) {
        userInfoEditor.putString(AvcConstants.USERACCOUNT,account).commit();
    }
   public String getUserAccount() {
        return userInfoSp.getString(AvcConstants.USERACCOUNT,"");
    }

    //手机
    public void setUserPhone(String phone) {
        userInfoEditor.putString(AvcConstants.USERPHONE,phone).commit();
    }
    public String getUserPhone() {
        return userInfoSp.getString(AvcConstants.USERPHONE,"");
    }

    //邮箱
    public void setUserEmail(String email) {
        userInfoEditor.putString(AvcConstants.USEREMAIL,email).commit();
    }
    public String getUserEmail() {
        return userInfoSp.getString(AvcConstants.USEREMAIL,"");
    }


    //密码
    public void setUserPassword(String password) {
        userInfoEditor.putString(AvcConstants.USERPASSWORD,password).commit();
    }
    public String getUserPassword() {
        return userInfoSp.getString(AvcConstants.USERPASSWORD,"");
    }


    //存储key
    public void setKey(String password) {
        userInfoEditor.putString(AvcConstants.USERKEY,password).commit();
    }
    public String getKey() {
        return userInfoSp.getString(AvcConstants.USERKEY,"");
    }

    //密码是否可见
    public void setPasswordShow(boolean type) {
        installEditor.putBoolean(AvcConstants.USERPASSWORDSHOW,type).commit();
    }
    public boolean getPasswordShow() {
        return installSp.getBoolean(AvcConstants.USERPASSWORDSHOW,false);
    }

    //记住密码
    public void setRememberPassword(boolean type) {
        installEditor.putBoolean(AvcConstants.USERPASSWORD,type).commit();
    }
    public boolean getRememberPassword() {
        return installSp.getBoolean(AvcConstants.USERPASSWORD,false);
    }

    //sessionId
    public void setSessionId(String sessionId) {
        userInfoEditor.putString(AvcConstants.USERSESSIONID,sessionId).commit();
    }
    public String getSessionId() {
        return userInfoSp.getString(AvcConstants.USERSESSIONID,"");
    }


    //最新周的选取
    public void setNewWeek(String sessionId) {
        userInfoEditor.putString(AvcConstants.USERGETNEWWEEK,sessionId).commit();
    }
    public String getNewWeek() {
        return userInfoSp.getString(AvcConstants.USERGETNEWWEEK,"");
    }


    //用户权限
    public void setUserLimits(String limit) {
        userInfoEditor.putString(AvcConstants.USERLIMIT,limit).commit();
    }
    public String getUserLimits() {
        return userInfoSp.getString(AvcConstants.USERLIMIT,"1");
    }


    /***
     * Online and offline------------1=>线上/2=>线下
     */
    public void  setLineType(int lineType){
        userInfoEditor.putInt("lineType",lineType).commit();
    }

    /***
     * Online and offline------------1=>线上/2=>线下
     */
    public int getLineType(){
        return userInfoSp.getInt("lineType",2);
    }

    /***
     * BasisRatio and LinkRelativeRatio------------1=>同比/2=>环比
     */
    public void  setRatioType(int lineType){
        userInfoEditor.putInt("ratioType",lineType).commit();
    }

    /***
     * BasisRatio and LinkRelativeRatio------------1=>同比/2=>环比
     */
    public int getRatioType(){
        return userInfoSp.getInt("ratioType",1);
    }

    /***
     * Set the selection cycle mode-----------
     */
    public void  setWeekType(String WeekType){
        userInfoEditor.putString("WeekType",WeekType).commit();
    }

    /***
     * Set the selection cycle mode------------
     */
    public String getWeekType(){
        return userInfoSp.getString("WeekType","当前周");
    }

    /***
     *Set the choice of sales pin mode------------1=>销量/2=>销额
     */
    public void  setSaleType(int saleType){
        userInfoEditor.putInt("saleType",saleType).commit();
    }

    /***
     *Set the choice of sales pin mode ------------ 1 => 销量 / 2 => 销额
     */
    public int getSaleType(){
        return userInfoSp.getInt("saleType",2);
    }

    /***
     * Set the token for login
     */
    public void setLoginToken(String mToken){
        userInfoEditor.putString("mToken",mToken).commit();
    }

    /***
     * Set the token for login
     */
    public String getLoginToken(){
        return userInfoSp.getString("mToken","");
    }

    /***
     * Set the user to select the competitive product
     * @param catecory catecory.
     * @param brands Brand set.
     */
    public void setCompetingBrands(String catecory,Set<String> brands){
        BaseLog.e("存储竞品","catecory:"+catecory+"<>Brands"+new Gson().toJson(brands));
        userInfoEditor.putStringSet(catecory,brands).commit();
    }

    /***
     * Set the user to select the competitive product
     * @param catecory catecory.
     * @return Brand set, if not set to return null.
     */
    public Set<String> getCompetingBrands(String catecory){
            return userInfoSp.getStringSet(catecory,null);
    }

    /***
     * Set the user to select the competitive product
     * @param catecories catecories.
     */
    public void setCompetingCategory(String catecories){
        BaseLog.e("存储竞品","catecories:"+catecories);
        userInfoEditor.putString("userCatecory",catecories).commit();
    }

    /***
     * Set the user to select the competitive product
     * @return catecory set, if not set to return null.
     */
    public String getCompetingCategroys(){
        return userInfoSp.getString("userCatecory",null);
    }

}
